import { useNavigation, useLocation } from 'react-router-dom'

type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'

// 获取导航过程中，是否正在提交请求
export const useNavSubmitting = (method: Method, pathname?: string) => {
  const navigation = useNavigation()
  const location = useLocation()

  const submitting = navigation.state === 'submitting' && navigation.formMethod?.toUpperCase() === method && navigation.formAction === (pathname || location.pathname)

  return submitting
}

// 获取导航过程中，是否正在刷新页面数据
export const useNavLoading = (method: Method, pathname?: string) => {
  const navigation = useNavigation()
  const location = useLocation()

  const loading = navigation.state === 'loading' && navigation.formMethod?.toUpperCase() === method && navigation.formAction === (pathname || location.pathname)
  return loading
}
